/* $Id: Splash.java,v 1.12 2007/09/07 06:51:21 gigabass Exp $
 *
 * Copyright (c) Guido Ludwig. All rights reserved.
 * Use is subject to license terms.
 */
package de.guidoludwig.af;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Image;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;



/**
 * Basic implementation for a splash screen
 * 
 * Uses an AWT Window to display a splash screen with an image and
 * a progress bar
 * 
 * @author <a href="mailto:jtrade@gigabss.de">Guido Ludwig</a>
 * @version $Revision: 1.12 $
 */
public final class Splash extends JWindow {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
    private String note;
    private JLabel label;
    
    
	private final Image image;
    private Color foreground;
    private Color background;
    private final String title; 
    
//    private static Splash INSTANCE = buildSplash();
//    
//    private static Splash buildSplash() {
//    	ImageIcon icon = SwingUtil.loadIcon(
//        "splash.jpg");
//    	return new Splash(icon == null ? null : icon.getImage(), "Splash initilalized");
//    }
    	

    public Splash(Image image, String title) {
        super(new Frame());
        this.image = image;
        foreground = Color.BLACK;
        background = Color.WHITE;
        this.title = title;
        label = new JLabel();
        configure();
    }

    private void configure() {
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(createCenteredComponent(title), BorderLayout.NORTH);
        getContentPane().add(new ImagePane(image), BorderLayout.CENTER);
        getContentPane().add(createCenteredComponent(label), BorderLayout.SOUTH);
        pack();
        centerOnScreen();
    }

    public void open() {
        setVisible(true);
    }

    public void close() {
        dispose();
    }

    public void setMessage(String message) {
        note = message;
        label.setText(note);
    }

    private void centerOnScreen() {
        Dimension mySize = getSize();
        Dimension screenSize = getToolkit().getScreenSize();
        setLocation((screenSize.width - mySize.width) / 2,
            (screenSize.height - mySize.height) / 2);
    }

    private Component createCenteredComponent(String text) {
        return createCenteredComponent(new JLabel(text));
    }
    
    private Component createCenteredComponent(JComponent comp) {
        comp.setOpaque(false);
        JPanel panel = new JPanel(new FlowLayout());
        panel.add(comp);
        panel.setBackground(background);
        panel.setForeground(foreground);
        comp.setForeground(foreground);
        panel.setBorder(BorderFactory.createEmptyBorder(1,1,1,1));
        return panel;
    }
}